package com.edwardyk.java.tools;

import java.util.HashMap;
import java.util.Map;

public class YKNotificationCenter {

    public class YKNotification {
        public String name;
        public Object object;
        public Map<String,Object> userInfo = null;
    }

    public interface YKNotificationCenterCallBack {
        void callBack(YKNotification notification);
    }

    private static YKNotificationCenter _defatult = new YKNotificationCenter();
    private YKNotificationCenter(){}
    private Map<String,YKNotificationCenterCallBack> list = new HashMap<>();

    //方法没有加同步块，所以它效率高
    public static YKNotificationCenter defaultCenter() { return _defatult; }

    public void postObserverForName(String name, Object object, Map<String,Object> userInfo)
    {
        if (this.list.containsKey(name))
        {
            YKNotificationCenterCallBack callBack = this.list.get(name);
            YKNotification notification = new YKNotification();
            notification.name = name;
            notification.object = object;
            notification.userInfo = userInfo;
            callBack.callBack(notification);

        }
    }

    public void postObserverForName(String name, Object object)
    {
        this.postObserverForName(name, object, new HashMap<String,Object>() {{}});
    }

    public void addObserver(String name, YKNotificationCenterCallBack callBack)
    {
        YKNotificationCenter.defaultCenter().list.put(name,callBack);
    }

    public void removeObserver(String name)
    {
        YKNotificationCenter.defaultCenter().list.remove(name);
    }



}
